#include "sqlconnection.h"

SqlConnection& SqlConnection::getInstance()
{
	static SqlConnection conn;
	return conn;
}

void SqlConnection::init(const QString& sqlType, const QString& connctionName)
{
	m_connctionName = connctionName;
	if (connctionName == "") {
		m_db = QSqlDatabase::addDatabase(sqlType);
	}
	else {
		m_db = QSqlDatabase::addDatabase(sqlType,connctionName);
	}
}

bool SqlConnection::openDB(const QString& dbFileName)
{
	m_db.setDatabaseName(dbFileName);
	if (!m_db.open()) {
		m_error = m_db.lastError().text();
		return false;
	}
	return true;
}

void SqlConnection::closeDB()
{
	if (m_db.isOpen()) {
		m_db.close();
		QSqlDatabase::removeDatabase(m_connctionName);
	}
}

QString SqlConnection::getError()
{
	return m_error;
}

SqlConnection::~SqlConnection()
{}
